#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#define ARGS_CHECK(argc, num)                 \
    {                                         \
        if (argc != num)                      \
        {                                     \
            fprintf(stderr, "args error!\n"); \
            return -1;                        \
        }                                     \
    }
#define ERROR_CHECK(ret, num, msg) \
    {                              \
        if (ret == num)            \
        {                          \
            perror(msg);           \
            exit(1);               \
        }                          \
    }
/**
 * @brief 
 * 
 * @param argc 
 * @param argv  ./  file   pipe  
 * @return int 
 */
int main(int argc, char *argv[])
{
    ARGS_CHECK(argc,3);
    int ret = mkfifo(argv[2],0664);
    FILE* fdsrc = fopen(argv[1],"r");
    ERROR_CHECK(NULL,fdsrc,"open src");
    int fdpipe = open(argv[2],O_WRONLY);
    ERROR_CHECK(-1,fdpipe,"open pipe");
    char srcBuf[32] = {0};

    while (ret = fread(srcBuf,1,32,fdsrc))
    {
         printf("%d\n",ret);
         puts(srcBuf);
         write(fdpipe,srcBuf,ret);
         memset(srcBuf,0,sizeof(srcBuf));
    }
    
    return 0;
}
